<template>
  <div v-loading="loading" class="list-table noPad" style="min-height:300px;">
    <el-tabs v-model="activeName" @tab-click="clickMenu">
      <el-tab-pane v-for="(item, key) in childMenuTabs" :key="key" :lazy="true" :label="$t(item.menuName)" :name="item.menuName" />
      <div v-loading="loading" class="voteMain">
        <!--动态页面 ，根据is的值来确定渲染哪个组件 -->
        <component :is="menuPath" ref="menuPath" :parameter="parameter" />
      </div>
    </el-tabs>
  </div>
</template>
<script>
import { childMenuList	} from '@/api/system/menu'
import { getAsyncComponent } from '@/utils'

export default {
  data() {
    return {
      // 加载
      loading: true,
      // 选中页签名称
      activeName: this.$t('对外投资'),
      // 项目ID
      companyId: this.$route.query.companyId,
      // 子页签
      childMenuTabs: [],
      // 组件地址
      menuPath: '',
      // 参数
      parameter: {},
      // 菜单标识
      menuIdent: ''
    }
  },
  created() {
    this.getMenuIdent()
    this.listLoad()
  },
  mounted() {
    this.listComponent()
  },
  methods: {
    // 根据来源，获取菜单标识
    getMenuIdent() {
      const source = this.$route.query.source
      if (source === 'BF_PORTFOLIO') {
        this.menuIdent = 'indirect_company'
      } else if (source === 'DEAL_DEBT') {
        this.menuIdent = 'debt_company'
      } else if (source === 'DEAL_EQUITY') {
        this.menuIdent = 'stock_company'
      } else {
        this.menuIdent = 'customer_company'
      }
    },
    listLoad() {
      this.loading = true
      // 菜单列表
      childMenuList(this.menuIdent).then(response => {
        this.childMenuTabs = response.rows
        this.clickMenu(response.rows[0])
        this.loading = false
      })
    },
    clickMenu(tab) {
      this.childMenuTabs && this.childMenuTabs.forEach(item => {
        if (item.menuName === tab.menuName || item.menuName === tab.name) {
          this.$nextTick(() => {
            this.$set(item, 'flag', true)
            this.importFormPath(item.component)
            this.load()
          })
        } else {
          this.$set(item, 'flag', false)
        }
      })
    },
    listComponent() {
      this.$bus.$once('async_done', () => {
        this.load()
      })
    },
    load() {
      if (this.$refs.menuPath?.init !== undefined) {
        const data = {
          id: this.companyId
        }
        this.$refs.menuPath.init(data)
        this.$refs.menuPath.openLocation = 'Home'
      }
    },
    // 引入组件
    importFormPath(menuPath) {
      if (menuPath) {
        this.menuPath = getAsyncComponent('@/views/' + menuPath)
        console.log(this.menuPath)
      } else {
        const error = 'error/404'
        this.importFormPath(error)
      }
    }
  }
}
</script>
